<?php
/**
 * Created by PhpStorm.
 * User: allen.qiang
 * Date: 2015/12/11
 * Time: 16:53
 * 短信发送类 移动助理平台
 *
 */


class quanliEcdoSms
{
    /*正常返回结果如下，1为正常发送，后面视为任务码，需维护进LOG表，
    非正常情况下只反回一个错误代码，见最上面定义。
     * [16-Dec-2015 18:34:43] Array
    (
        [code] => 200
        [data] => 1,201512161834241740
    )
     */
    /*
     * 返回状态码
     *
     */
    public $code = array(
        -1 => '用户名或者密码不正确',
         1 => '发送成功',
        2 => '余额不够',
        3 => '扣费失败(请联系客服)',
        5 => '短信定时成功',
        6 => '有效号码为空',
        7 => '短信内容为空',
        8 => '无签名,必须,格式:【签名】',
        9 => '没有Url提交权限',
        10 => '发送号码过多,最多支持200个号码',
        11 => '产品ID异常',
        12 => '参数异常',
        15 => 'Ip验证失败',
        19 => '短信内容过长，最多支持500个字符',
        20 => '定时时间不正确:格式:20130202120212(14位数字)'
    );

    /**
     * 构造方法
     * @param object 传递应用的app
     * @return null
     */
    public function __construct()
    {

    }

    /*
    *
    * 发送接口
    * @params  手机号 发送内容 手机号数量 报错信息 延时时间
    * @return  bool || msg
    *
     */

    public function sendSMS($mobile,$content,$type)
    {

        global $G_SHOP;

        $username = '';
        $productid ='';
        /**************************验证码专用***************************/
        if($type==1){
            //账号用户名
            $username = 'zjl_dgys';
            //账号密码
            $password = 'dgys12345';
            $productid = '676767';
        }
        /**************************通知类专用***************************/
        if($type==2){
            $username = 'tz_dgys';
            //账号密码
            $password = 'dgys12345';
            $productid = '191919';//通知类专用
        }

        //记录短信发送记录
        $log_sms = new entity('log_sms');
        $log_sms->creator = 'system';
        $log_sms->mobile = $mobile;
        $log_sms->sms_content = $content;
        $log_sms->ip = get_ip();


        $content1 = iconv("UTF-8","UTF-8",$content);
        $this->url = 'http://www.ztsms.cn:8800/sendSms.do';
        quanli_require('module/base/common/quanliHttp.class.php');
        $http = new quanliHttp();
        $result_array = $http->get('http://www.ztsms.cn:8800/sendSms.do?productid='.$productid.'&username='.$username.'&password='.$password.'&mobile='.$mobile.'&content='.$content1);
        error_log(print_r($result_array,1));
        //HTTP请求失败
        if ($result_array['code'] != 200) {
            $log_sms->sms_status = 2;
            $log_sms->sms_fail_content = print_r($result_array, true);
            $G_SHOP->DBCA->save($log_sms);
            return array( 'code' => 404, 'message' => '短信发放失败' );
        }

        $tmp = explode(',', $result_array['data']);

        if ($tmp[0] != 1 && $result_array) {
            error_log('短信发放失败：' .$this->code[$tmp[0]] );
            $log_sms->sms_status = 2;
            $log_sms->sms_fail_content = $this->code[$tmp[0]];
            $G_SHOP->DBCA->save($log_sms);
            return array( 'code' => 404, 'message' => '短信发放失败' );
        }

        //短信任务号
        $log_sms->taskId = $tmp[1];
        $log_sms->sms_status = 1;
        $G_SHOP->DBCA->save($log_sms);
        return array( 'code' => 200, 'message' => '短信发送成功' );


    }


}